package com.pphh.demo;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

/**
 * memory leak demo with infinite class loader
 * <p>
 * test command
 * java -XX:MetaspaceSize=1m -XX:MaxMetaspaceSize=1m -classpath "./target/classes" com.pphh.demo.DemoClassLoader
 *
 * @author huangyinhuang
 * @date 8/23/2018
 */
public class DemoClassLoader {

    public static void main(String[] args) {

        Long i = 0L;
        List<ClassLoader> classLoaderList = new ArrayList<>();
        try {
            String path = DemoClassLoader.class.getResource("/").getPath();
            URL url = new File(path).toURI().toURL();
            while (true) {
                ClassLoader loader = new URLClassLoader(new URL[]{url});
                loader.loadClass("com.pphh.demo.Demo");
                classLoaderList.add(loader);
                Thread.sleep(10);
                System.out.println(i++);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
